/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.newnode.view;

import offset.nodes.client.dialog.newnode.model.ContentType;
import java.awt.Color;
import java.util.ResourceBundle;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import offset.nodes.client.model.PanelContainer;
import offset.nodes.client.model.Validator;

/**
 *
 * @author  Walter Lütgenau
 */
public class FilePanel extends javax.swing.JPanel implements Validator {
    JFileChooser chooser = null;
    ContentType[] contentTypes;
    PanelContainer container;

    public FilePanel() {
    }

    /** inits new form FileObjectPanel */
    public void init(ContentType[] contentTypes, PanelContainer container) {
        this.contentTypes = contentTypes;
        this.container = container;
        initComponents();
        contentTypeCombo.setModel(new DefaultComboBoxModel(contentTypes));        
        contentTypeDescription.setText(((ContentType)contentTypeCombo.getSelectedItem()).getDescription() );
    }
    
    public boolean isValidPane() {
        boolean isValid = false;
        
        if ( nameTextField.getText() != null && nameTextField.getText().length() > 0 && filePathTextField.getText() != null && filePathTextField.getText().length() > 0)
            isValid = true;
        
        if ( !isValid ) {
            JOptionPane messageDialog = new JOptionPane();
            messageDialog.showMessageDialog(this, ResourceBundle.getBundle("offset/nodes/dialog/view/DialogBundle").getString("file.step1.invalid"));
        }
                    
        return isValid;
    }
    
    public String getFileName() {
        return nameTextField.getText();
    }
    
    public String getFilePath() {
        return filePathTextField.getText();
    }
    
    public String getContentType() {
        if ( contentTypeCombo.getSelectedIndex() >= 0)
            return ((ContentType) contentTypeCombo.getSelectedItem()).getMimeType();
        return null;
    }

    public JFileChooser getFileChooser() {
        if (chooser == null)
            chooser = new JFileChooser();

        return chooser;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        filePathLabel = new javax.swing.JLabel();
        filePathTextField = new javax.swing.JTextField();
        filePathButton = new javax.swing.JButton();
        contentTypeLabel = new javax.swing.JLabel();
        contentTypeCombo = new javax.swing.JComboBox();
        nameLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        jSeparator2 = new javax.swing.JSeparator();
        contentTypeDescription = new javax.swing.JEditorPane();

        setMinimumSize(new java.awt.Dimension(411, 305));

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("offset/nodes/client/dialog/view/DialogBundle"); // NOI18N
        filePathLabel.setText(bundle.getString("fileObject.filePathLabel")); // NOI18N

        filePathButton.setText(bundle.getString("fileObject.selectButton")); // NOI18N
        filePathButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doSelectFile(evt);
            }
        });

        contentTypeLabel.setText(bundle.getString("fileObject.contentTypeLabel")); // NOI18N

        contentTypeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "none", "binary", "css", "html", "xsd" }));
        contentTypeCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                contentTypeComboActionPerformed(evt);
            }
        });

        nameLabel.setText(bundle.getString("fileObject.nameLabel")); // NOI18N

        contentTypeDescription.setEditable(false);
        contentTypeDescription.setBackground(new Color(0,0,0,0));
        contentTypeDescription.setFont(nameLabel.getFont());
        contentTypeDescription.setOpaque(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 513, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(contentTypeLabel)
                            .addComponent(filePathLabel)
                            .addComponent(nameLabel))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(contentTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(contentTypeDescription))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(filePathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 334, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(filePathButton)))
                                .addGap(64, 64, 64))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(148, 148, 148))))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(nameTextField))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(filePathLabel)
                    .addComponent(filePathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(filePathButton))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(contentTypeLabel)
                            .addComponent(contentTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(69, 69, 69))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(contentTypeDescription)))
                .addGap(40, 40, 40))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void doSelectFile(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doSelectFile
        if (chooser == null)
            chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int result = chooser.showOpenDialog(this);
        if ( result != JFileChooser.APPROVE_OPTION)
            return;
        filePathTextField.setText(chooser.getSelectedFile().getPath());
        String name = chooser.getSelectedFile().getName();
        int index = name.lastIndexOf(".");
        if ( index >= 0) {
            String suffix = name.substring(index+1);
            for( int i=0; i<contentTypeCombo.getItemCount(); i++) {
                if ( suffix.toLowerCase().equals(((ContentType)contentTypeCombo.getItemAt(i)).getExtension().toLowerCase()) ) {
                    contentTypeCombo.setSelectedIndex(i);
                    break;
                }
            }
        }
        nameTextField.setText(name);
        if (container != null && isValidPane())
            container.enableOkButton(true);
    }//GEN-LAST:event_doSelectFile

    private void contentTypeComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_contentTypeComboActionPerformed
        contentTypeDescription.setText(((ContentType)contentTypeCombo.getSelectedItem()).getDescription());
    }//GEN-LAST:event_contentTypeComboActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox contentTypeCombo;
    private javax.swing.JEditorPane contentTypeDescription;
    private javax.swing.JLabel contentTypeLabel;
    private javax.swing.JButton filePathButton;
    private javax.swing.JLabel filePathLabel;
    private javax.swing.JTextField filePathTextField;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    // End of variables declaration//GEN-END:variables
    
}
